package org.example.spring.self.demo.s3;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * 文件描述
 *
 * @author yezhou41127
 * @date: 2022年08月01日
 */
@Component
public class MyBeanPostProcessor implements InstantiationAwareBeanPostProcessor, DestructionAwareBeanPostProcessor {

	@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		if ("lifeCycleBean".equals(beanName))
			System.out.println("instantiation before");
		// 初始化执行之前 这里返回的对象会替换掉原本的 bean
		return null;
	}

	@Override
	public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
		if ("lifeCycleBean".equals(beanName))
			System.out.println("instantiation after");
		// 初始化执行之后 如果返回 false 会跳过 依赖注入
//		return false;
		return true;
	}

	@Override
	public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
		// 依赖注入
		if ("lifeCycleBean".equals(beanName))
			System.out.println("postProcessProperties");
		return pvs;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if ("lifeCycleBean".equals(beanName))
			System.out.println("init before");
		// PostConstruct
		// ConfigurationProperties
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if ("lifeCycleBean".equals(beanName)) System.out.println("init after");
		// 代理增强
		return bean;
	}

	@Override
	public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
		if ("lifeCycleBean".equals(beanName)) System.out.println("销毁之前");
	}

}
